<template>
	<view class="app-content">
		<SeachNav @search="searchKey" @openPopup="openPopup"></SeachNav>
		<raiders-filter ref="raidersFilter" @filterQuery="filterQuery" :filter-mask-data.sync="filterMaskData" @filterBtnCall="filterBtnCall" />

		<counsel-list ref="counsel" v-if="!list.loading" :list="list" :counselData="counselData" @goDetail="goDetail" @toMaterialList="toMaterialList" @pushIteam="pushIteam" @popularize="popularize"></counsel-list>

		<add-com-icon @add="addRaiders" v-if="userInfo.role_id !=2" />
	</view>
</template>

<script>
import SeachNav from '@/pages/components/SeachNav.vue'
import RaidersFilter from '@/pages/components/RaidersFilter.vue'
import CounselList from '@/pages/getGuest/components/CounselList.vue'
import AddComIcon from '@/components/module/AddComIcon.vue'

import { getArticleList } from '@/api/getGuest'

import { list } from '@/mixins'
import { mapGetters } from 'vuex'
import { getArticleSelectList } from '@/api/getGuest'
export default {
	name: 'winArticle',
	mixins: [list],
	components: {
		SeachNav,
		CounselList,
		RaidersFilter,
		AddComIcon
	},
	data() {
		return {
			cloneFilterMaskData: [],
			form: {
				cat_id: '',
				is_public: ''
			},
			filterMaskData: [
				{
					title: '一级分类',
					list: []
				},
				{
					title: '二级分类',
					list: []
				},
				{
					title: '标签',
					list: [],
					hide: false
				},
				{
					title: '来源',
					list: [],
					hide: false
				}
			],
			counselData: {
				title: '获客文章',
				type: 'article',
				shareType: 3,
				myKey: {
					id: 'article_id',
					images: 'cover_img',
					title: 'title',
					status: 'cat_name'
				}
			}
		}
	},
	computed: {
		...mapGetters({ userInfo: 'getUser' })
	},
	onLoad(option) {
		this.option = option
		this.init()
	},
	// 下拉刷新
	onPullDownRefresh() {
		let that = this
		this.resetList()
		this.getArticle(this.option).then(res => {
			uni.stopPullDownRefresh()
		})
	},
	// 加载更多
	onReachBottom() {
		if (this.list.loading) {
			return
		}
		if (this.list.data.length < this.list.total) {
			this.getArticle(this.option).then(res => {
				uni.stopPullDownRefresh()
			})
		} else {
			uni.stopPullDownRefresh()
			uni.showToast({
				title: '没有更多了',
				duration: 2000,
				icon: 'none'
			})
		}
	},
	methods: {
		// ===初始化===
		init() {
			this.getArticle()
			this.getSelectList()
		},

		// ===搜索===
		searchKey(val) {
			this.form.name = val
			this.resetList()
			this.getArticle()
		},
		openPopup() {
			this.$refs.raidersFilter.$refs.screen.openPopup()
		},
		// ===筛选===
		getSelectList() {
			let that = this
			getArticleSelectList().then(res => {
				if (res.status === 'success') {
					that.filterMaskData[0].list = that.$refs.raidersFilter.formatSelect(res.data.class_list)
					that.filterMaskData[2].list = that.$refs.raidersFilter.formatTypeSelect(res.data.type_list)
					that.filterMaskData[3].list = that.$refs.raidersFilter.formatTypeSelect(res.data.source_type_list)
				}
			})
		},
		// ===递归筛选===
		filterBtnCall(items) {
			if (items.value === 'multi') {
				this.filterMaskData[1].list = items.list
			}
			if (items.value === 'type_list') {
				this.form.is_public = items.status ? items.id : ''
			} else if (items.value === 'source_type') {
				this.form.source_type = items.status ? items.id : ''
			} else {
				this.form.cat_id = items.status ? items.id : ''
			}
			this.resetList()
			this.getArticle()
		},
		// ===重置筛选===
		filterQuery(val) {
			this.filterMaskData[1].list = []
			this.getSelectList()
			this.form = {
				cat_id: '',
				is_public: '',
				source_type: ''
			}
			console.log(this.filterMaskData)
			this.resetList()
			this.getArticle()
		},

		// ===列表数据===
		getArticle() {
			let that = this
			return getArticleList({
				page: that.list.page,
				limit: that.list.limit,
				...that.form
			}).then(res => {
				that.completes(res)
			})
		},

		// 朋友圈推广
		toMaterialList(data, type) {
			let param = this.$refs.counsel.forParams(data)
			let params = {
				...param,
				relevance_id: data.article_relevance_id,
				case_article_id: data.article_id,
				resource_type: 1,
				imgUrl: data.cover_img,
				cat_name: data.cat_name,
				from: 'caseOrArticle'
			}
			uni.navigateTo({
				url: `/pages/getGuest/shareList${this.commonJS.queryData(params)}`
			})
		},
		// 推给团队
		pushIteam(data, type) {
			let param = this.$refs.counsel.forParams(data)
			let params = {
				...param,
				status: data.cat_name,
				imgUrl: param.images,
				type: 1,
				from_origin: 1
			}

			uni.navigateTo({
				url: `/pages/getGuest/pushTeam${this.commonJS.queryData(params)}`
			})
		},
		// 推广获客
		popularize(val) {
			this.$nextTick(() => {
				this.$refs.counsel.$refs.sharePoster.showMask(
					{
						id: val[this.counselData.myKey.id],
						url: true
					},
					true
				)
			})
		},
		// 进入详情
		goDetail(val) {
			let query = {
				mainId: val.article_id,
				articleid: 'null',
				owner_user_id: this.option.owner_user_id
			}
			console.log(query)
			uni.navigateTo({
				url: `/pages/getGuest/articledetail${this.commonJS.queryData(query)}`
			})
		},
		// ===新增文章===
		addRaiders() {
			// 跳转webview --------------------
			let token = uni.getStorageSync('token')
			const params = {
				owner_user_id: this.userInfo.user_id,
				token: token,
				miniapp_path: `/pages/getGuest/winArticle?owner_user_id=${this.userInfo.user_id}`,
				type: 'hkds_miniapp'
			}
			const returnUrl = encodeURIComponent(`${this.$constant.formal}/hkds/#/admin/article/addTxt${this.$util.parse(params)}`)
			console.log(decodeURIComponent(returnUrl))
			// return false
			uni.navigateTo({
				url: `/pages/page/webview/index?returnUrl=${returnUrl}&user_id=${this.userInfo.user_id}`
			})
			// this.$router.push({
			// 	name: 'addTxt',
			// 	query: {
			// 		owner_user_id: this.userInfo.user_id
			// 	}
			// })
		}
	}
}
</script>

<style lang="scss" scoped>
</style>
